
package repositorios;

import interfaces.IRepositorioEmpresa;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import modelos.Empresa;
import util.GerenteConexao;


public class RepositorioEmpresa implements IRepositorioEmpresa{

	public void adicionarEmpresa( Empresa e){

		String sql = "insert into empresa(id, cnpj, nome, site, email, telefone, contato) values(?,?,?,?,?,?,?)";

		try {
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			montarOperacao(ps, e);

			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	public void removerEmpresa(Empresa e){

		String sql = "delete from empresa where id = ?";

		try{
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			ps.setLong(1, e.getId());

			ps.executeUpdate();
		} catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public List<Empresa> listarEmpresa(){

		String sql = "Select * from empresa";

		List<Empresa> retorno = null;
		try {
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			retorno = new ArrayList<Empresa>();

			while(rs.next()){
				Empresa e = montarObjeto(rs);
				retorno.add(e);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

	public void editarEmpresa(Empresa e){
		String sql = "update empresa set id=?, cnpj =?, nome =?, site=?, email=?, telefone=?, contato=?  where id = "+e.getId();

		try {
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			montarOperacao(ps, e);

			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public Empresa buscarEmpresaPorId(Long id){
		String sql = "select * from empresa where id = " + id;
		Empresa e = null;

		try{
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				e = montarObjeto(rs);
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return e;
	}
	
	public List<Empresa> buscarLike(String campo, String valor){
		String sql = "Select * FROM empresa WHERE " + campo + " like '%" + valor + "%'";

		List<Empresa> retorno = null;
		try {
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			retorno = new ArrayList<Empresa>();

			while(rs.next()){
				Empresa e = montarObjeto(rs);
				retorno.add(e);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}
	
	private Empresa montarObjeto(ResultSet rs){
		Empresa e = new Empresa();
		try {
			e.setId(rs.getLong("id"));
			e.setCnpj(rs.getString("cnpj"));
			e.setNome(rs.getString("nome"));
			e.setSite(rs.getString("site"));
			e.setEmail(rs.getString("email"));
			e.setTelefone(rs.getString("telefone"));
			e.setContato(rs.getString("contato"));
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return e;
	}
	
	private void montarOperacao(PreparedStatement ps, Empresa e){

		try {
			ps.setLong(1, e.getId());
			ps.setString(2, e.getCnpj());
			ps.setString(3, e.getNome());
			ps.setString(4, e.getSite());
			ps.setString(5, e.getEmail());
			ps.setString(6, e.getTelefone());
			ps.setString(7, e.getContato());
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
}
